package com.ourteam.product.requirement.api;

import org.apache.commons.lang.StringUtils;

import com.ourteam.frame.web.AbstractSearchModel;
import com.ourteam.product.requirement.dao.ProductGlossaryDAOQueryBean;
import com.ourteam.product.requirement.dao.ProductVersionRelateDAOFactory;
import com.ourteam.product.requirement.dao.ProductVersionRelateDAOQueryBean;
import com.ourteam.product.requirement.domain.ProductElementTypeEnum;
import com.ourteam.product.requirement.domain.ProductGlossaryBean;

public class ProductGlossarySearchModel extends AbstractSearchModel<ProductGlossaryDAOQueryBean> {

	private long productDefineId;

	private long productVersionId;

	private String name;

	private String content;
	
	private String type;
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public long getProductVersionId() {
		return productVersionId;
	}

	public void setProductVersionId(long productVersionId) {
		this.productVersionId = productVersionId;
	}

	public long getProductDefineId() {
		return productDefineId;
	}

	public void setProductDefineId(long productDefineId) {
		this.productDefineId = productDefineId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Override
	public String getMappedDAOField(String aProperty) {
		return ProductGlossaryBean.getMappedDAOField(aProperty);
	}

	@Override
	protected void doBuildQueryBean(ProductGlossaryDAOQueryBean queryBean) throws Exception {

		ProductGlossaryDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		if (productDefineId != 0) {
			criteria.andProductIdEqualTo(productDefineId);
		}

		if (productVersionId != 0) {
			ProductVersionRelateDAOQueryBean versionRelateQueryBean = new ProductVersionRelateDAOQueryBean();

			versionRelateQueryBean.addProductElementIdSelectProperty();

			versionRelateQueryBean.createCriteria().andProductVersionIdEqualTo(productVersionId)
					.andProductElementTypeEqualTo(ProductElementTypeEnum.PRODUCT_GLOSSARY.getName());

			criteria.andIdIn(versionRelateQueryBean, ProductVersionRelateDAOFactory.getProductVersionRelateDAO());
		}

		if (StringUtils.isNotBlank(name)) {
			criteria.andNameLike(name);
		}

		if (StringUtils.isNotBlank(content)) {
			criteria.andContentLike(content);
		}
		
		if (StringUtils.isNotBlank(type)) {
			criteria.andTypeEqualTo(type);
		}

	}

}
